/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *                                                                         *
 *   AndroidWorld Library, Copyright 2011 Bryan Chadwick                   *
 *                                                                         *
 *   FILE: ./android/image/RegularPolygon.java                             *
 *                                                                         *
 *   This file is part of AndroidWorld.                                    *
 *                                                                         *
 *   AndroidWorld is free software: you can redistribute it and/or         *
 *   modify it under the terms of the GNU General Public License           *
 *   as published by the Free Software Foundation, either version          *
 *   3 of the License, or (at your option) any later version.              *
 *                                                                         *
 *   AndroidWorld is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with AndroidWorld.  If not, see <http://www.gnu.org/licenses/>. *
 *                                                                         *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

package android.image;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;

/**
 * <style type='text/css'><!--
 *    .com{ font-style: italic; color: #880000; }
 *    .keyw{ font-weight: bold; color: #000088; }
 *    .num{ color: #00AA00; }
 *    .str{ color: #CC00AB; }
 *    .prim{ color: #0000FF; }
 *    img.example{ padding-left: 50px; padding-bottom: 30px; }
 *  --></style>
 *
 * Represents an Image of a Regular Polygon.
 *     
 * <pre>
 *    <span class='keyw'>new</span> RegularPolygon(<span class='num'>50</span>, <span class='num'>3</span>, <span class='str'>"outline"</span>, <span class='str'>"red"</span>)</pre>
 * <img class="example" src="test/images/polygon-1.png" />
 * <br />
 *
 * <pre>
 *    <span class='keyw'>new</span> RegularPolygon(<span class='num'>40</span>, <span class='num'>4</span>, <span class='str'>"outline"</span>, <span class='str'>"blue"</span>)</pre>
 * <img class="example" src="test/images/polygon-2.png" />
 * <br />
 *
 * <pre>
 *    <span class='keyw'>new</span> RegularPolygon(<span class='num'>20</span>, <span class='num'>8</span>, <span class='str'>"solid"</span>, <span class='str'>"red"</span>)</pre>
 * <img class="example" src="test/images/polygon-3.png" />
 * <br />
 *
 */
public class RegularPolygon extends Image{
    protected int width;
    protected int height;
    protected int mode;
    protected Paint paint;
    protected Path path;
    
    /** Create a Regular Polygon Image with (double) radius, sides, mode and color */
    public RegularPolygon(double radius, int sides, String mode, String color){
        this(round(radius),sides,mode,color);
    }
    /** Create a Regular Polygon Image with (int) radius, sides, skip, mode and color */
    public RegularPolygon(int radius, int sides, String mode, String color){
        this(radius,sides,mode(mode),color(color));
    }
    /** Create a Regular Polygon Star with converted mode and color */
    protected RegularPolygon(int radius, int sides, int mode, int color){
        this(make(radius, sides), mode, color);
    }
    private static Path make(int radius, int sides){
        Path poly = new Path();
        double first = (sides%2==0)?Math.PI/sides:3*Math.PI/2,
               mult = 2*Math.PI/sides;
        poly.moveTo((int)(radius*Math.cos(first)),
                    (int)(radius*Math.sin(first)));
        for(int i = 1; i < sides; i++){
            poly.lineTo((int)(radius*Math.cos(first+i*mult)),
                        (int)(radius*Math.sin(first+i*mult)));
        }
        poly.close();
        return poly;
    }
    /** Create a Regular Polygon Star with converted mode and color */
    protected RegularPolygon(Path poly, int mode, int color){
        super(0,0);
        this.mode = mode;
        this.paint = painter(color,mode);
        this.path = poly;
        RectF r = new RectF();
        poly.computeBounds(r, false);
        width = (int)(r.right-r.left);
        height = (int)(r.bottom-r.top);
        pinholeX = width/2;
        pinholeY = height/2;
        path.offset(-(r.left+(width+r.left))/2,
                    -(r.top+(height+r.top))/2);
    }
    /** Paint this Polygon into the given graphics */
    public void paint(Canvas c, int x, int y){
        Path t = new Path(path);
        t.offset(x, y);
        c.drawPath(t, paint);
    }
    /** Return the Width of this Image */
    public int width(){ return width; }
    /** Return the Height of this Image */
    public int height(){ return height; }
}
